<?php 
	session_start();
        require_once '../funciones.php';
        $con = con_caweb();
//
//
//
//    $soporte=$_SESSION['caweb']['soporte'];
//
//	include("dbconfig.php");
        $fechaIni   = ($_REQUEST['fechaini'] != "")?$_REQUEST['fechaini']:'1986-01-01';
        $fechaFin   = ($_REQUEST['fechaFin'] != "")?$_REQUEST['fechaFin']:'2999-01-01';
        $nit        = $_REQUEST['nit'];
        $factura    = $_REQUEST['factura'];
        $sucursal   = $_REQUEST['sucursal'];
        //Se construye la condicion
        $where = ($nit != "")?" and fac.cliente = '$nit'":"";
        $where2 = ($factura != "")?" and abo.factura = '$factura'":"";
        $where1 = ($sucursal != "")?" and rec.sucursal = '$sucursal'":"";


	$page = $_REQUEST['page']; 

	// get how many rows we want to have into the grid - rowNum parameter in the grid 
	$limit = $_REQUEST['rows']; 

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel 
	$sidx = $_REQUEST['sidx']; 

	// sorting order - at first time sortorder 
	$sord = $_REQUEST['sord']; 

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1; 

//	// connect to the MySQL database server
//	$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
//
//	// select the database
//	mysql_select_db($database) or die("Error connecting to db.");

	// calculate the number of rows for the query. We need this for paging the result
        $sql = "select fac.facturasistema,fac.sucursal,abo.fechaabono,fac.fechapago,fac.factura,fac.fechafactura,fac.base,fac.iva,fac.total,fac.nombre,fac.retefuente,fac.reteiva,fac.reteica,fac.otros,fac.cuadre,fac.cuadre_base,abo.valor as valorpagado,fac.observacioncartera,
                fac.estadocartera,rec.tipo,rec.banco,rec.cuenta,rec.cheque from recaudos rec
                inner join abonosfacturacion abo on abo.recaudo = rec.recaudo $where2
                inner join cabeceras_fac fac on fac.facturasistema = abo.facturasistema and fac.factura <> 0 and fac.estado != 'A' $where
                where rec.fecharecaudo between '$fechaIni' and '$fechaFin' $where1";
        //(fac.cuadre_base - fac.iva - fac.retefuente - fac.reteiva - fac.reteica) as recaudoBase,
	$result = mysql_query($sql,$con) or die("Error SQL 1".  mysql_error().$sql);
	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$count = mysql_num_rows($result);

	// calculate the total pages for the query 
	if( $count > 0 ) { 
				  $total_pages = ceil($count/$limit); 
	} else { 
				  $total_pages = 0; 
	} 

	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 

	// the actual query for the grid data
//	$SQL = "SELECT * FROM abonosfacturacion WHERE transaccion > 0 GROUP BY transaccion ORDER BY $sidx $sord LIMIT $start , $limit";

	$SQL = $sql ." ORDER BY $sidx $sord LIMIT $start , $limit";

	$result = mysql_query( $SQL ) or die("Error SQL 2.".mysql_error().$SQL);

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
//        echo "<sql>".$SQL."</sql>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";
        echo "<row id=''>";
        echo "<cell></cell><cell></cell><cell></cell><cell></cell>";
        echo "<cell>Totales</cell>";
        $sql = "SELECT SUM( fac.base ) AS base, SUM( fac.iva ) AS iva, SUM( fac.total ) AS total, SUM( fac.retefuente ) AS rtefuente, SUM( fac.reteiva ) AS rteiva, SUM( fac.reteica ) AS rteica, SUM( fac.otros ) AS otros, SUM( abo.valor ) AS valorpagado
                FROM recaudos rec
                INNER JOIN abonosfacturacion abo ON abo.recaudo = rec.recaudo $where2
                INNER JOIN cabeceras_fac fac ON fac.facturasistema = abo.facturasistema
                AND fac.factura <>0
                AND fac.estado !=  'A' $where
                WHERE rec.fecharecaudo
                BETWEEN '$fechaIni' and '$fechaFin' $where1";
        $result1 = mysql_query($sql, $con) or die("Error SQL 3".mysql_error().$sql);

        echo "<cell>". number_format(mysql_result($result1, 0,"base"),0, ',', '.')."</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"iva"),0, ',', '.')."</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"total"),0, ',', '.')."</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"rtefuente"),0, ',', '.')."</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"rteiva"),0, ',', '.')."</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"rteica"),0, ',', '.')."</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"otros"),0, ',', '.')."</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"valorpagado"),0, ',', '.')."</cell>";
        //(sum(fac.cuadre_base)-sum(fac.iva)-sum(fac.retefuente)-sum(fac.reteiva)-sum(fac.reteica)) as recaudoBase,
//        echo "<cell>". number_format(mysql_result($result1, 0,"recaudoBase"),0, ',', '.')."</cell>";
        echo "<cell></cell><cell></cell><cell></cell><cell></cell><cell></cell><cell></cell>";
        echo "</row>";
//        echo "<sql>$sql</sql>";

	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		echo "<row id='". $row['facturasistema']."'>";
		echo "<cell>". $row['sucursal']."</cell>";
		echo "<cell>". $row['fechaabono']."</cell>";
		echo "<cell>". $row['factura']."</cell>";
                echo "<cell>". $row['fechafactura']."</cell>";
                echo "<cell>". utf8_encode(htmlspecialchars($row['nombre']))."</cell>";
                echo "<cell>". number_format($row['base'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['iva'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['total'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['retefuente'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['reteiva'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['reteica'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['otros'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['valorpagado'],0, ',', '.')."</cell>";
//                echo "<cell>". number_format($row['recaudoBase'],0, ',', '.')."</cell>";
                echo "<cell>". utf8_encode(htmlspecialchars($row['observacioncartera']))."</cell>";
                echo "<cell>". $row['estadocartera']."</cell>";
                echo "<cell>". $row['tipo']."</cell>";
                echo "<cell>". $row['banco']."</cell>";
                echo "<cell>". $row['cuenta']."</cell>";
                echo "<cell>". $row['cheque']."</cell>";
		echo "</row>";
	}
	echo "</rows>";
	 
?>
